import path from "path";
import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
// element_plus icon图标自动引入插件
import Icons from "unplugin-icons/vite";
import IconsResolver from "unplugin-icons/resolver";
// element_plus组件自动引入插件
import AutoImport from "unplugin-auto-import/vite";
import Components from "unplugin-vue-components/vite";
import { ElementPlusResolver } from "unplugin-vue-components/resolvers";
// svg图标插件
import { createSvgIconsPlugin } from "vite-plugin-svg-icons";
// mock数据相关插件
import { viteMockServe } from "vite-plugin-mock";
// import Inspect from "vite-plugin-inspect";

const originalPath = path.resolve(__dirname, "./src");
console.log(__dirname);
// https://vitejs.dev/config/
export default defineConfig(({ command }) => ({
  resolve: {
    alias: {
      "@": originalPath,
    },
  },
  plugins: [
    vue(),
    createSvgIconsPlugin({
      // Specify the icon folder to be cached
      iconDirs: [path.resolve(process.cwd(), "src/assets/icons")],
      // Specify symbolId format
      symbolId: "icon-[dir]-[name]",
    }),
    viteMockServe({
      // @ts-ignore
      localEnabled: command === "serve",
    }),
    AutoImport({
      // Auto import functions from Vue, e.g. ref, reactive, toRef...
      // 自动导入 Vue 相关函数，如：ref, reactive, toRef 等
      imports: ["vue"],
      dts: path.resolve(originalPath, "auto-imports.d.ts"),
    }),
    AutoImport({
      // Auto import functions from Element Plus, e.g. ElMessage, ElMessageBox... (with style)
      // 自动导入 Element Plus 相关函数，如：ElMessage, ElMessageBox... (带样式)
      resolvers: [
        ElementPlusResolver(),

        // Auto import icon components
        // 自动导入图标组件
        IconsResolver({
          prefix: "Icon",
        }),
      ],

      dts: path.resolve(originalPath, "auto-imports.d.ts"),
    }),

    Components({
      resolvers: [
        // Auto register icon components
        // 自动注册图标组件
        IconsResolver({
          enabledCollections: ["ep"],
        }),
        // Auto register Element Plus components
        // 自动导入 Element Plus 组件
        ElementPlusResolver(),
      ],

      dts: path.resolve(originalPath, "components.d.ts"),
    }),

    Icons({
      autoInstall: true,
    }),

    // Inspect(),
  ],
  css: {
    preprocessorOptions: {
      scss: {
        javascriptEnabled: true,
        additionalData: '@import "/src/styles/variable.scss";',
      },
    },
  },
}));
